﻿using System;
using System.Collections.Generic;
using BrainTechLLC;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight.AltSerializer.ServiceReference1;
using AltSerialize;
using ClassesToSerialize;

namespace Silverlight.AltSerializer
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Service1Client svc = new Service1Client();
            svc.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(svc_DoWorkCompleted);
            svc.DoWorkAsync();
        }

        public void OutputText(string s)
        {
            Dispatcher.BeginInvoke(() => OutputTextUnsafe(s));
        }

        private void OutputTextUnsafe(string s)
        {
            txtOutput.Text += Environment.NewLine + s;
        }

        void svc_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            Service1Client svc = sender as Service1Client;
            svc.DoWorkCompleted -= svc_DoWorkCompleted;

            Serializer s = new Serializer();
            byte[] decompressed = e.Result.Decompress();
            List<DemoMonster> results = s.Deserialize(decompressed, typeof(List<DemoMonster>)) as List<DemoMonster>;
            List<Item> items = new List<Item>();
            results.ForEach((m) => items.AddRange(m.Items));

            OutputText(string.Format("Read {0} demo monsters, {1} Items", results.Count, items.Count));
            OutputText(string.Format("{0} bytes / {1} bytes decompressed/compressed", decompressed.Length, e.Result.Length));

            svc.CloseAsync();
        }
    }
}
